//引入组件库
var nf = sm("do_Notification");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var qqHttp = require("helper/http");
var helperOpen = require("helper/open");
var communityUtil = require("helper/community");
var circleHelper = require("helper/circle");
var userUrl = require("url/user");
var communityUrl = require("url/community");
var circleUrl = require("url/circle");
var mallUrl = require("url/mall");

var listdata = mm("do_ListData");

var headerLeft=ui("headerLeft");
var alayoutRoot=ui("alayoutRoot");
var textFieldSearch=ui("textFieldSearch");
var do_Label_1=ui("do_Label_1");
var listView = ui("listView");
listView.bindItems(listdata);

//本页面-ajax业务请求url
var httpUrl = communityUrl.url.base.distancePage;
var httpCommunityUrl = communityUrl.url.base.distancePage;
var httpUserUrl = userUrl.url.base.page;
var httpCircleUrl = circleUrl.url.base.search;
var httpMallUrl = mallUrl.url.shop.search
var httpProductUrl = mallUrl.url.product.searchPro
var noData = [ {
	"template" : 1
} ];
//定义变量
var search;
var searchType=1;//1社区、2用户、3圈子、4店铺、5商品
var page = 1;

var sendBtn=ui("sendBtn");
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

headerLeft.on("touch", function() {
	do_App.closePage()
});

var menus = [ {
	"template" : 0,
	"menuIcon":"source://image/menu/sea_01.png",
	"menuName" : "社区",
	"type" : "community",
	"isLineBg" : true
}, {
	"template" : 0,
	"menuIcon":"source://image/menu/sea_02.png",
	"menuName" : "用户",
	"type" : "user",
	"isLineBg" : true
}, {
	"template" : 0,
	"menuIcon":"source://image/menu/sea_03.png",
	"menuName" : "圈子",
	"type" : "circle",
	"isLineBg" : true
}, {
	"template" : 0,
	"menuIcon":"source://image/menu/sea_04.png",
	"menuName" : "店铺",
	"type" : "mall",
	"isLineBg" : true
} , {
	"template" : 0,
	"menuIcon":"source://image/menu/sea_05.png",
	"menuName" : "商品",
	"type" : "product",
	"isLineBg" : false
} ];

/*
 * 菜单弹出
 */
var popmenu = alayoutRoot.add("menupop", "source://view/reuse/menu/menu2.ui", 0, 0);
var popmenuf = ui("menupop");

ui("do_ALayout_search").on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});
popmenuf.on("popmenuItem", function(data) {
	popmenuf.fire("popmenuClose");
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	do_Label_1.text=menus[touchIndex].menuName;
	if (actionType == "community") {// 发大喇叭
		searchType=1;
		httpUrl = httpCommunityUrl;
	}else if(actionType == "user"){
		searchType=2;
		httpUrl = httpUserUrl;
	}else if(actionType == "circle"){
		searchType=3;
		httpUrl = httpCircleUrl;
	}else if(actionType == "mall"){
		searchType=4;
		httpUrl = httpMallUrl;
	}else if(actionType == "product"){
		searchType=5;
		httpUrl = httpProductUrl;
	}
	textFieldSearch.text="";
	search=null;
	loadPage();
});

textFieldSearch.on("enter", function(data, e) {
	sendSearch();
});
sendBtn.on("touch", function(data, e) {
	sendSearch();
});
function sendSearch(){
	do_Page.hideKeyboard();
	search = textFieldSearch.text;
	if(null!=search&&''!=search){
		loadPage();
	}else{
		nf.toast("请输入关键字");
	}
}
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	loadPage();
});

function loadPage(){
	page = 1;
	var params = {
		page : page,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if(data.page.content){
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].template=searchType;
			}
			listdata.addData(data.page.content);
		}
		else{
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}

//刷新数据
function getNextPage() {
	page++;
	var params = {
		page : page,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		if(data.page.content){
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].template=searchType;
			}
			listdata.addData(data.page.content);
		}
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPage();
	}
});
//点击一个社区
listView.on("touch", function(data){
	var onRow = listdata.getOne(data);
	var id = onRow.id;
	if(searchType==1){
		communityUtil.communityVisitId(id);
		do_App.closePage({"fromPage":"search"});
	}else if(searchType==2){
		openHelper.openPersonal(id);
	}else if(searchType==3){
		circleHelper.toCircleDetail(id,onRow.name);
	}else if(searchType==4){
		helperOpen.open("source://view/qshop/detail/main.ui",{shopId:id});
	}else if(searchType==5){
		helperOpen.open("source://view/mall/product/main.ui",{"id":id});
	}
});